package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import Auxiliary.Sequence_String;
import DataView.EnumerationInfo;
import Store.Enumeration;

public class CD_ManageEnumeration 
{
	JPanel container;
	ShowClassDiagram classDiagram;
	CampoTesto nome = new CampoTesto();
	CampoTesto parametro = new CampoTesto();
	JPanel centerPanel;
	JTable tab;
	String selected;
	Sequence_String listaParametri = new Sequence_String();
	boolean modifica;
	Enumeration originalElement;
	
	public JPanel show(ShowClassDiagram cd, boolean modifica, Enumeration enume)
	{
		classDiagram = cd;
		this.modifica=modifica;
		originalElement=enume;
		
		container = new JPanel();
		container.setLayout(new BorderLayout());
		String  str="";
		if(modifica)
			str=enume.getName();
		container.add(getIntestazione(str),BorderLayout.NORTH);
		
		if(enume!=null)
			listaParametri=enume.getInfo().getLiterals().clone();
		container.add(getCenterArea(),BorderLayout.CENTER);
		container.add(getButtonArea(), BorderLayout.SOUTH);
		
		return container;
	}

	private JPanel getIntestazione(String namestr) 
	{
		JPanel pannelloIntestazione = new JPanel();
		pannelloIntestazione.setLayout(new BorderLayout());
		JLabel label = new JLabel("New Enumeration");
		if(modifica) label.setText("Modify Enumeration");
		label.setFont(new Font("Tahoma", Font.BOLD, 18));
		pannelloIntestazione.add(label, BorderLayout.NORTH);
		if(modifica) 
		{
			nome.crea("Inserisci qui il nome");
			nome.setText(namestr);
			pannelloIntestazione.add(nome.get(), BorderLayout.CENTER);
		}
		else pannelloIntestazione.add(nome.crea("Insert Enumeration Name"), BorderLayout.CENTER);
		pannelloIntestazione.add(new JLabel(), BorderLayout.SOUTH);
		
		return pannelloIntestazione;
	}

	private JPanel getCenterArea() 
	{
		centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		
		JPanel northPanel= new JPanel();
		northPanel.setLayout(new GridLayout(1,3));
		JButton addParam = new JButton("add Literal");
		addParam.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(parametro.getText().length()==0)
					showMessage("Insert name for Literal");
				else{
				listaParametri.UNordered_add(parametro.getText());
				parametro.clear();
				refresh();
			}}
		});
		JButton delParam = new JButton("delete Literal");
		delParam.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				listaParametri.remove(selected);
				refresh();
			}
		});
		
		northPanel.add(parametro.crea("Literal Name"));
		northPanel.add(addParam);
		northPanel.add(delParam);
		
		centerPanel.add(northPanel, BorderLayout.NORTH);
		centerPanel.add(getTable(), BorderLayout.CENTER);
		return centerPanel;
	}

	@SuppressWarnings("serial")
	private JPanel getTable() 
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		tab = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}};
		modelloRicerca.addColumn("Present Literals");
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tab.setFillsViewportHeight(true);
		
		int max = 0;
		if(listaParametri!=null)
			max=listaParametri.size();
		for (int i= 0; i<max; i++)
		{
			modelloRicerca.addRow(new Object[]{listaParametri.get(i)});						
		}
		
		tabella.setLayout(new BorderLayout());
		tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tab, BorderLayout.CENTER);
		
		tab.addMouseListener(new MouseAdapter() 
		{
			public void mouseClicked(MouseEvent e)
		    {
		        if (e.getClickCount() == 1)
		        	try
		        	{
		        		int row = tab.getSelectedRow(); 
		        		selected = tab.getValueAt(row, 0).toString();
	        		}
		        	catch (Exception ArrayIndexOutOfBoundsException) {}
		    }
		});
		
		return tabella;
	}

	private JPanel getButtonArea() 
	{
		JPanel buttonPanel = new JPanel();
		JButton ok = new JButton("Save");
		JButton cancel = new JButton("Cancel");
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.add(ok);
		buttonPanel.add(cancel);
		
		ok.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				EnumerationInfo info = new EnumerationInfo();
				info.setInfo(nome.getText(), listaParametri);
				if(info.getName().length()==0)
					showMessage("Insert the name for enumaration");
				else{
					if(info.getLiterals().size()==0){
						showMessage("Insert a literals");
					}
					else{
						classDiagram.container.removeAll();
						if(modifica) classDiagram.updateInputCD.updCDElement(originalElement, info);
						else classDiagram.updateInputCD.addCDElement(info);
					}
					}
			}
		});
		cancel.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent e) 
			{
				classDiagram.container.removeAll();
				classDiagram.showCD();
			}
		});
		
		return buttonPanel;
	}
	
	public void refresh()
	{
		centerPanel.removeAll();
		centerPanel.add(getCenterArea(), BorderLayout.CENTER);
		centerPanel.revalidate();
	}

	public void showMessage(String msg)
	{
		JOptionPane.showMessageDialog(container, msg, "Error", JOptionPane.ERROR_MESSAGE);
	}
}
